/**
 * 
 */
package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.common.BtoECalendarDialog;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.common.MyTimeUtil;
import cn.com.winning.ecare.common.PullDownView;
import cn.com.winning.ecare.common.PullDownView.OnPullDownListener;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.utils.Utils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

import com.readystatesoftware.viewbadger.BadgeView;

/**就诊中历史消息查询
 * @author ZJJ
 */
public class HisNotificationDetail extends Activity implements OnPullDownListener, OnItemClickListener
{

	private HisNotificationDetail oThis = this;
	private static final int WHAT_DID_LOAD_DATA_ERR = -1;
	private static final int WHAT_DID_LOAD_DATA = 0;
	private static final int WHAT_DID_REFRESH = 1;
	private static final int WHAT_DID_MORE = 2;
	private PullDownView mPullDownView;
	private ListView mListView;
	private List<Map<String, Object>> grouplist = new ArrayList<Map<String, Object>>();
	GroupAdapter groupAdapter = null;
	CustomProgressDialog proDialog;// 加载框
	// 分页加载
	private int pageSize = 20;
	private static final int pageLoadMore = 20;
	private MyApplication myApplication;
	private String startDate = "", endDate = "";
	private TextView hzxm, startDateText, endDateText;
	private ImageButton menuButton, mainselected, hztx;
	private LinearLayout layout;
	private int height, statusBarHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.hisnotificationpush);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);
		/*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
		mPullDownView = (PullDownView) findViewById(R.id.pull_down_view);
		mPullDownView.setOnPullDownListener(this);
		mListView = mPullDownView.getListView();
		mListView.setDivider(null);
		groupAdapter = new GroupAdapter();
		mListView.setAdapter(groupAdapter);
		findView();
		setListener();
		new Thread(new InitData_()).start();
	}

	private void findView()
	{
		menuButton = (ImageButton) findViewById(R.id.menuButton);
		mainselected = (ImageButton) findViewById(R.id.mainselected);
		hztx = (ImageButton) findViewById(R.id.hztx);
		startDateText = (TextView) findViewById(R.id.startDateText);
		endDateText = (TextView) findViewById(R.id.endDateText);
		startDate = MyTimeUtil.GetSysDate(MyTimeUtil.yyyy_MM_dd, MyTimeUtil.DateToStr(MyTimeUtil.yyyy_MM_dd, new Date()), 0, 0, 0);
		endDate = MyTimeUtil.DateToStr(MyTimeUtil.yyyy_MM_dd, new Date());
		startDateText.setText(startDate);
		endDateText.setText(endDate);
		hzxm = (TextView) findViewById(R.id.hzxm);
		hzxm.setText(myApplication.getHzxm() == null ? "" : myApplication.getHzxm());
		layout = (LinearLayout) findViewById(R.id.fylayout);

	}

	private void setListener()
	{

		menuButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				oThis.onBackPressed();
			}
		});
		mainselected.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});
		startDateText.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				initDateDialog();
			}
		});
		endDateText.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				initDateDialog();
			}
		});
		hztx.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				mListView.post(new Runnable()
				{

					@Override
					public void run()
					{
						mListView.setSelectionAfterHeaderView();
					}
				});
			}
		});
	}

	private void initDateDialog()
	{

		// 初始化一个自定义的Dialog
		final BtoECalendarDialog dialog = new BtoECalendarDialog(oThis, R.style.MyDialog,startDateText.getText().toString(),endDateText.getText().toString());
		// 获取状态栏高度
		Rect frame = new Rect();
		getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		statusBarHeight = frame.top;
		// 获取layout高度
		height = layout.getHeight();
		dialog.show();
		Window mWindow = dialog.getWindow();
		WindowManager windowManager = getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.x = 0;
		lp.y = height + statusBarHeight;
		lp.width = (int) (display.getWidth()); // 设置宽度
		lp.height = (int) (display.getHeight()) - height - statusBarHeight;// 设置高度
		mWindow.setAttributes(lp);

		Button btnok = (Button) dialog.findViewById(R.id.btnok);
		Button btncancel = (Button) dialog.findViewById(R.id.btncancel);
		// 选择开始日期
		final TextView startDates = (TextView) dialog.findViewById(R.id.tvSelectedstart);
		startDates.setText(startDate);
		// 选择结束日期
		final TextView endDates = (TextView) dialog.findViewById(R.id.tvSelectedend);
		endDates.setText(endDate);
		btnok.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// // 选择开始日期
				// TextView startDates = (TextView)
				// dialog.findViewById(R.id.tvSelectedstart);
				// // 选择结束日期
				// TextView endDates = (TextView)
				// dialog.findViewById(R.id.tvSelectedend);
				String sd = startDates.getText().toString();
				String ed = endDates.getText().toString();

				dialog.dismiss();
				startDate = sd;
				endDate = ed;
				startDateText.setText(startDate);
				endDateText.setText(endDate);
				pageSize = 20;
				new Thread(new InitData_()).start();
			}
		});

		btncancel.setOnClickListener(new View.OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				dialog.dismiss();
			}
		});
	}

	private void onLoad()
	{
		mPullDownView.notifyDidLoad();
		mPullDownView.notifyDidRefresh();
		mPullDownView.notifyDidMore();
		mPullDownView.enableAutoFetchMore(true, 1);
	}

	private Handler myHandler = new Handler()
	{

		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			switch (msg.what)
			{
				case WHAT_DID_LOAD_DATA_ERR:
					Toast.makeText(oThis, (String) msg.obj, Toast.LENGTH_SHORT).show();
					grouplist.clear();
					mListView.removeFooterView(mPullDownView.getmFooterView());
					groupAdapter.notifyDataSetChanged();// 更新ExpandableListView
					onLoad();
					break;
				case WHAT_DID_LOAD_DATA:
				{
					if (msg.obj != null)
					{
						grouplist = (List<Map<String, Object>>) msg.obj;
					}
					groupAdapter.notifyDataSetChanged();// 更新ExpandableListView
					onLoad();
					break;
				}
				case WHAT_DID_REFRESH:
				{
					if (msg.obj != null)
					{
						grouplist = (List<Map<String, Object>>) msg.obj;
					}
					groupAdapter.notifyDataSetChanged();// 更新ExpandableListView
					onLoad();
					break;
				}

				case WHAT_DID_MORE:
				{
					if (msg.obj != null)
					{
						grouplist = (List<Map<String, Object>>) msg.obj;
					}

					groupAdapter.notifyDataSetChanged();// 更新ExpandableListView
					onLoad();
					break;
				}
			}

		}

	};

	private void initData()
	{
		Message msg = myHandler.obtainMessage();
		List<Map<String, Object>> templist = new ArrayList<Map<String, Object>>();
		JSONArray jsonArr;
		List<NameValuePair> ls = new ArrayList<NameValuePair>();
		ls.add(new BasicNameValuePair("hisStartDate", startDate));
		ls.add(new BasicNameValuePair("hisEndDate", endDate));
		ls.add(new BasicNameValuePair("accountname", myApplication.getAccount()));
		ls.add(new BasicNameValuePair("mobilephone", myApplication.getSjh()));
		ls.add(new BasicNameValuePair("hospitalcode", myApplication.getYydm()));
		ls.add(new BasicNameValuePair("start", "0"));
		ls.add(new BasicNameValuePair("limit", String.valueOf(pageSize)));
		JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.PUSHSERVERFINDHISLIST, ls);
		try
		{
			if (json != null)
			{
				jsonArr = json.getJSONArray("hislist");
				for (int i = 0; i < jsonArr.length(); i++)
				{
					JSONObject jsonObject = jsonArr.getJSONObject(i);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("title", "消息");
					map.put("message", jsonObject.getString("message"));
					map.put("time", jsonObject.getString("sendDate"));
					map.put("xh", jsonObject.getString("id"));
					map.put("type", jsonObject.getString("messagetype"));
					if (isJsonarray(map))
					{
						templist.add(map);
					}
				}
				if (templist.size() > 0)
				{
					msg.what = WHAT_DID_LOAD_DATA;
					msg.obj = templist;
				}
				else
				{
					msg.what = WHAT_DID_LOAD_DATA_ERR;
					msg.obj = "暂无数据";
				}

			}
			else
			{
				msg.what = WHAT_DID_LOAD_DATA_ERR;
				msg.obj = "暂无数据";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			msg.what = WHAT_DID_LOAD_DATA_ERR;
			msg.obj = "暂无数据";
			myHandler.sendMessage(msg);
		}

	}

	private void refreshData()
	{
		Message msg = myHandler.obtainMessage();
		List<Map<String, Object>> templist = new ArrayList<Map<String, Object>>();
		JSONArray jsonArr;
		List<NameValuePair> ls = new ArrayList<NameValuePair>();
		ls.add(new BasicNameValuePair("hisStartDate", startDate));
		ls.add(new BasicNameValuePair("hisEndDate", endDate));
		ls.add(new BasicNameValuePair("accountname", myApplication.getAccount()));
		ls.add(new BasicNameValuePair("mobilephone", myApplication.getSjh()));
		ls.add(new BasicNameValuePair("hospitalcode", myApplication.getYydm()));
		ls.add(new BasicNameValuePair("start", "0"));
		ls.add(new BasicNameValuePair("limit", String.valueOf(pageSize)));
		JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.PUSHSERVERFINDHISLIST, ls);
		try
		{
			if (json != null)
			{
				jsonArr = json.getJSONArray("hislist");
				for (int i = 0; i < jsonArr.length(); i++)
				{
					JSONObject jsonObject = jsonArr.getJSONObject(i);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("title", "消息");
					map.put("message", jsonObject.getString("message"));
					map.put("time", jsonObject.getString("sendDate"));
					map.put("xh", jsonObject.getString("id"));
					map.put("type", jsonObject.getString("messagetype"));
					if (isJsonarray(map))
					{
						templist.add(map);
					}
				}

				if (templist.size() > 0)
				{
					msg.what = WHAT_DID_LOAD_DATA;
					msg.obj = templist;
				}
				else
				{
					msg.what = WHAT_DID_LOAD_DATA_ERR;
					msg.obj = "暂无更多数据";
				}
			}
			else
			{
				msg.what = WHAT_DID_LOAD_DATA_ERR;
				msg.obj = "暂无数据";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			msg.what = WHAT_DID_LOAD_DATA_ERR;
			msg.obj = "暂无数据";
			myHandler.sendMessage(msg);
		}
	}

	private void loadMoreData()
	{
		Message msg = myHandler.obtainMessage();
		List<Map<String, Object>> templist = new ArrayList<Map<String, Object>>();
		JSONArray jsonArr;
		List<NameValuePair> ls = new ArrayList<NameValuePair>();
		ls.add(new BasicNameValuePair("hisStartDate", startDate));
		ls.add(new BasicNameValuePair("hisEndDate", endDate));
		ls.add(new BasicNameValuePair("accountname", myApplication.getAccount()));
		ls.add(new BasicNameValuePair("mobilephone", myApplication.getSjh()));
		ls.add(new BasicNameValuePair("hospitalcode", myApplication.getYydm()));
		ls.add(new BasicNameValuePair("start", "0"));
		ls.add(new BasicNameValuePair("limit", String.valueOf(pageSize)));
		JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.PUSHSERVERFINDHISLIST, ls);
		try
		{
			if (json != null)
			{
				jsonArr = json.getJSONArray("hislist");
				for (int i = 0; i < jsonArr.length(); i++)
				{
					JSONObject jsonObject = jsonArr.getJSONObject(i);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("title", "消息");
					map.put("message", jsonObject.getString("message"));
					map.put("time", jsonObject.getString("sendDate"));
					map.put("xh", jsonObject.getString("id"));
					map.put("type", jsonObject.getString("messagetype"));
					if (isJsonarray(map))
					{
						templist.add(map);
					}
				}

				if (templist.size() > 0)
				{
					msg.what = WHAT_DID_LOAD_DATA;
					msg.obj = templist;
				}
				else
				{
					msg.what = WHAT_DID_LOAD_DATA_ERR;
					msg.obj = "暂无数据";
				}
			}
			else
			{
				msg.what = WHAT_DID_LOAD_DATA_ERR;
				msg.obj = "暂无数据";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			msg.what = WHAT_DID_LOAD_DATA_ERR;
			msg.obj = "暂无数据";
			myHandler.sendMessage(msg);
		}
	}

	private boolean isJsonarray(Map<String, Object> map)
	{
		boolean flag = true;
		try
		{
			String json = '[' + map.get("message").toString() + ']';
			JSONArray array = new JSONArray(json);
			JSONObject jsonObject = array.getJSONObject(0);
			if (map.get("type").toString().equals("100"))
			{// 排队
				map.get("xh").toString();
				jsonObject.getString("type");
				jsonObject.getString("num");
				jsonObject.getString("lastNum");
				jsonObject.getString("weizhi");
				jsonObject.getString("time");
			}
			else if (map.get("type").toString().equals("200"))
			{// 挂号
				map.get("xh").toString();
				jsonObject.getString("fy");
				jsonObject.getString("time");
				jsonObject.getString("ysmc");
				jsonObject.getString("ghlx");
				jsonObject.getString("ks");
				jsonObject.getString("num");
				jsonObject.getString("dhrs");
			}
			else if (map.get("type").toString().equals("300"))
			{// 费用
				// {jfdz:'缴费地址：东三楼一楼大厅左侧',jfje:'150',items:[{name:'血生化',je:'98.5'},{name:'血常规',je:'30'},{name:'心电图',je:'21.5'}]}
				map.get("xh").toString();
				jsonObject.getString("jfdz");
				jsonObject.getString("jfje");

				JSONArray arrayitems = new JSONArray(jsonObject.getString("items"));

				// 费用明细
			}
			else if (map.get("type").toString().equals("301"))
			{// 费用
				// {jfdz:'缴费地址：东三楼一楼大厅左侧',jfje:'150',items:[{name:'血生化',je:'98.5'},{name:'血常规',je:'30'},{name:'心电图',je:'21.5'}]}
				map.get("xh").toString();
				jsonObject.getString("zyh");
				jsonObject.getString("bq");
				jsonObject.getString("zyrq");
				jsonObject.getString("dqrq");
				jsonObject.getString("je");
				JSONArray arrayitems = new JSONArray(jsonObject.getString("items"));

				// 费用明细
			}
			else if (map.get("type").toString().equals("400"))
			{// 导航
				// {dhbt:'检查结果已完成',dhdz:'东五楼',dhmdd:'检验窗口',dhmb:'处取检验报告'}

				map.get("xh").toString();

				jsonObject.getString("dhbt");
				jsonObject.getString("dhbt");
				jsonObject.getString("dhdz");
				jsonObject.getString("dhmdd");
				jsonObject.getString("dhmb");
			}
			else
			{// 其他提醒
				map.get("xh").toString();

			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			flag = false;

		}
		return flag;
	}

	private class GroupAdapter extends BaseAdapter
	{

		private ItemsAdapter itemsAdapter;
		private ListView tempListView;

		@Override
		public int getCount()
		{

			return grouplist.size();
		}

		@Override
		public Object getItem(int position)
		{

			return grouplist.get(position);
		}

		@Override
		public long getItemId(int position)
		{

			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{

			View view = convertView;
			final Map<String, Object> map = (Map<String, Object>) getItem(position);

			try
			{
				String json = '[' + map.get("message").toString() + ']';
				JSONArray array = new JSONArray(json);
				JSONObject jsonObject = array.getJSONObject(0);
				// jsonObject.getString("name");

				if (map.get("type").toString().equals("100"))
				{// 排队
					view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_pd_item, null);
					TextView tv01 = (TextView) view.findViewById(R.id.push_pd_item_tv01);// 排队名（XX排队提醒）
					TextView tv02 = (TextView) view.findViewById(R.id.push_pd_item_tv04);// 当前号（您是XX号）
					TextView tv03 = (TextView) view.findViewById(R.id.push_pd_item_tv06);// 当前正在就诊号（29号正在就诊）
					TextView tv04 = (TextView) view.findViewById(R.id.push_pd_item_tv07);// 候诊位置（候诊位置：一号楼三楼302室）
					TextView tv05 = (TextView) view.findViewById(R.id.push_pd_item_tv08);// 预约就诊时间（预约就诊时间
					TextView senddate = (TextView) view.findViewById(R.id.push_pd_item_senddate);
					senddate.setText(map.get("time").toString());
					CheckBox push_pd_item_cb = (CheckBox) view.findViewById(R.id.push_pd_item_cb); // 12:35）
					push_pd_item_cb.setVisibility(View.INVISIBLE);
					tv01.setText(jsonObject.getString("type"));
					tv02.setText(jsonObject.getString("num"));
					tv03.setText(jsonObject.getString("lastNum"));
					tv04.setText(jsonObject.getString("weizhi"));
					tv05.setText(jsonObject.getString("time"));
					BadgeView badge = new BadgeView(getApplicationContext(), push_pd_item_cb);
					badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
					badge.setTextColor(Color.BLACK);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setText(String.valueOf(position + 1));
					badge.show();
				}
				else if (map.get("type").toString().equals("200"))
				{// 挂号
					view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_gh_item, null);
					TextView tv01 = (TextView) view.findViewById(R.id.push_gh_item_tv02); // 挂号费用(挂号费用XX元)
					TextView tv02 = (TextView) view.findViewById(R.id.push_gh_item_tv04);// 当前时间（）
					TextView tv03 = (TextView) view.findViewById(R.id.push_gh_item_tv05); // 医生名称
					TextView tv04 = (TextView) view.findViewById(R.id.push_gh_item_tv06);// 挂号类型
					TextView tv05 = (TextView) view.findViewById(R.id.push_gh_item_tv07);// 所挂科室
					TextView tv06 = (TextView) view.findViewById(R.id.push_gh_item_tv09);// 当前号(您是XX号)
					TextView tv07 = (TextView) view.findViewById(R.id.push_gh_item_tv12);// 前面有多少号（您前面还有XXX位在等候）
					TextView senddate = (TextView) view.findViewById(R.id.push_pd_item_senddate);
					senddate.setText(map.get("time").toString());
					CheckBox push_pd_item_cb = (CheckBox) view.findViewById(R.id.push_gh_item_cb); // 12:35）
					push_pd_item_cb.setVisibility(View.INVISIBLE);
					tv01.setText(jsonObject.getString("fy"));
					tv02.setText(jsonObject.getString("time"));
					tv03.setText(jsonObject.getString("ysmc"));
					tv04.setText(jsonObject.getString("ghlx"));
					tv05.setText(jsonObject.getString("ks"));
					tv06.setText(jsonObject.getString("num"));
					tv07.setText(jsonObject.getString("dhrs"));
					BadgeView badge = new BadgeView(getApplicationContext(), push_pd_item_cb);
					badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
					badge.setTextColor(Color.BLACK);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setText(String.valueOf(position + 1));
					badge.show();
				}
				else if (map.get("type").toString().equals("300"))
				{// 费用
					// {jfdz:'缴费地址：东三楼一楼大厅左侧',jfje:'150',items:[{name:'血生化',je:'98.5'},{name:'血常规',je:'30'},{name:'心电图',je:'21.5'}]}
					view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_fy_item, null);
					TextView tv01 = (TextView) view.findViewById(R.id.push_fy_item_tv01); // 缴费地址（缴费地址：东三楼一楼大厅左侧）
					TextView tv02 = (TextView) view.findViewById(R.id.push_fy_item_tv03);// 缴费金额（缴费金额XX元）
					TextView senddate = (TextView) view.findViewById(R.id.push_pd_item_senddate);
					senddate.setText(map.get("time").toString());
					Button zxff = (Button) view.findViewById(R.id.zxff);
					zxff.setVisibility(View.INVISIBLE);
					CheckBox push_pd_item_cb = (CheckBox) view.findViewById(R.id.push_fy_item_cb); // 12:35）
					push_pd_item_cb.setVisibility(View.INVISIBLE);
					tv01.setText(jsonObject.getString("jfdz"));
					tv02.setText(jsonObject.getString("jfje"));
					BadgeView badge = new BadgeView(getApplicationContext(), push_pd_item_cb);
					badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
					badge.setTextColor(Color.BLACK);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setText(String.valueOf(position + 1));
					badge.show();

					JSONArray arrayitems = new JSONArray(jsonObject.getString("items"));
					List<Map<String, Object>> childs = new ArrayList<Map<String, Object>>();

					for (int j = 0; j < arrayitems.length(); j++)
					{
						JSONObject child = arrayitems.getJSONObject(j);
						childs.add(Utils.getMap(child.toString()));
					}
					tempListView = (ListView) view.findViewById(R.id.push_fy_item_list);

					itemsAdapter = new ItemsAdapter(childs);
					tempListView.setAdapter(itemsAdapter);
					setListViewHeightBasedOnChildren(tempListView);
					itemsAdapter.notifyDataSetChanged();
					// 费用明细
				}
				else if (map.get("type").toString().equals("301"))
				{// 费用
					// {jfdz:'缴费地址：东三楼一楼大厅左侧',jfje:'150',items:[{name:'血生化',je:'98.5'},{name:'血常规',je:'30'},{name:'心电图',je:'21.5'}]}
					view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_yrqd_item, null);
					TextView tv01 = (TextView) view.findViewById(R.id.push_yrqd_item_tv01); // 缴费地址（缴费地址：东三楼一楼大厅左侧）
					TextView tv02 = (TextView) view.findViewById(R.id.push_yrqd_item_tv02);// 缴费金额（缴费金额XX元）
					TextView tv03 = (TextView) view.findViewById(R.id.push_yrqd_item_tv03);// 缴费金额（缴费金额XX元）
					TextView tv04 = (TextView) view.findViewById(R.id.push_yrqd_item_tv04);// 缴费金额（缴费金额XX元）
					TextView tv05 = (TextView) view.findViewById(R.id.push_yrqd_item_tv05);// 缴费金额（缴费金额XX元）
					TextView tv06 = (TextView) view.findViewById(R.id.push_yrqd_item_tv06);// 缴费金额（缴费金额XX元）
					TextView tv07 = (TextView) view.findViewById(R.id.push_yrqd_item_tv07);// 缴费金额（缴费金额XX元）
					TextView tv08 = (TextView) view.findViewById(R.id.push_yrqd_item_tv08);// 缴费金额（缴费金额XX元）
					TextView senddate = (TextView) view.findViewById(R.id.push_pd_item_senddate);
					senddate.setText(map.get("time").toString());
					Button zxff = (Button) view.findViewById(R.id.zxff);
					zxff.setVisibility(View.INVISIBLE);
					CheckBox push_pd_item_cb = (CheckBox) view.findViewById(R.id.push_yrqd_item_cb); // 12:35）
					push_pd_item_cb.setVisibility(View.INVISIBLE);
					BadgeView badge = new BadgeView(getApplicationContext(), push_pd_item_cb);
					badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
					badge.setTextColor(Color.BLACK);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setText(String.valueOf(position + 1));
					badge.show();
					tv01.setText(jsonObject.getString("zyh"));
					tv02.setText(jsonObject.getString("bq"));
					tv03.setText(jsonObject.getString("zyrq"));
					tv05.setText(jsonObject.getString("dqrq"));
					tv07.setText(jsonObject.getString("je"));

					JSONArray arrayitems = new JSONArray(jsonObject.getString("items"));
					List<Map<String, Object>> childs = new ArrayList<Map<String, Object>>();

					for (int j = 0; j < arrayitems.length(); j++)
					{
						JSONObject child = arrayitems.getJSONObject(j);
						childs.add(Utils.getMap(child.toString()));
					}

					tempListView = (ListView) view.findViewById(R.id.push_yrqd_item_list);
					itemsAdapter = new ItemsAdapter(childs);
					tempListView.setAdapter(itemsAdapter);
					setListViewHeightBasedOnChildren(tempListView);
					itemsAdapter.notifyDataSetChanged();
					// 费用明细
				}
				else if (map.get("type").toString().equals("400"))
				{// 导航
					// {dhbt:'检查结果已完成',dhdz:'东五楼',dhmdd:'检验窗口',dhmb:'处取检验报告'}
					view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_dh_item, null);
					TextView tv01 = (TextView) view.findViewById(R.id.push_dh_item_tv01); // 导航标题（检查结果已完成）
					TextView tv02 = (TextView) view.findViewById(R.id.push_dh_item_tv02);// 导航标题（检查结果已完成，请到）
					TextView tv03 = (TextView) view.findViewById(R.id.push_dh_item_tv03); // 导航地址
																																								// （东五楼）
					TextView tv04 = (TextView) view.findViewById(R.id.push_dh_item_tv04); // 导航目的地（检验窗口）
					TextView tv05 = (TextView) view.findViewById(R.id.push_dh_item_tv05); // 导航目标（处取检验报告）
					TextView senddate = (TextView) view.findViewById(R.id.push_pd_item_senddate);
					senddate.setText(map.get("time").toString());
					CheckBox push_pd_item_cb = (CheckBox) view.findViewById(R.id.push_dh_item_cb); // 12:35）
					push_pd_item_cb.setVisibility(View.INVISIBLE);
					tv01.setText(jsonObject.getString("dhbt"));
					tv02.setText(jsonObject.getString("dhbt") + ",请到");
					tv03.setText(jsonObject.getString("dhdz"));
					tv04.setText(jsonObject.getString("dhmdd"));
					tv05.setText(jsonObject.getString("dhmb"));
					BadgeView badge = new BadgeView(getApplicationContext(), push_pd_item_cb);
					badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
					badge.setTextColor(Color.BLACK);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setText(String.valueOf(position + 1));
					badge.show();
				}
				else
				{// 其他提醒
					view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_tx_item, null);

					TextView tv01 = (TextView) view.findViewById(R.id.push_tx_item_tv01);// 提醒标题
					TextView tv02 = (TextView) view.findViewById(R.id.push_tx_item_tv02);// 提醒时间
					TextView tv03 = (TextView) view.findViewById(R.id.push_tx_item_tv03);// 提醒内容
					TextView senddate = (TextView) view.findViewById(R.id.push_pd_item_senddate);
					senddate.setText(map.get("time").toString());
					CheckBox push_pd_item_cb = (CheckBox) view.findViewById(R.id.push_tx_item_cb); // 12:35）
					push_pd_item_cb.setVisibility(View.INVISIBLE);
					tv01.setText((CharSequence) map.get("title").toString());
					tv02.setText((CharSequence) map.get("time").toString());
					tv03.setText(jsonObject.getString("message"));
					BadgeView badge = new BadgeView(getApplicationContext(), push_pd_item_cb);
					badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
					badge.setTextColor(Color.BLACK);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setText(String.valueOf(position + 1));
					badge.show();

				}
			}
			catch (Exception e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
			return view;
		}

	}

	// 根据子项的高度重新设定主listview的item项高度
	public static void setListViewHeightBasedOnChildren(ListView listView)
	{
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null)
		{
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++)
		{
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

	private class ItemsAdapter extends BaseAdapter
	{
		private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		public ItemsAdapter(List<Map<String, Object>> templist)
		{
			list = templist;
		}

		@Override
		public int getCount()
		{

			return list.size();
		}

		@Override
		public Object getItem(int position)
		{

			return list.get(position);
		}

		@Override
		public long getItemId(int position)
		{

			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent)
		{

			View view = convertView;
			Map<String, Object> map = (Map<String, Object>) getItem(position);
			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.push_item_list, null);
			TextView tv01 = (TextView) view.findViewById(R.id.push_items_list_tv_01);
			TextView tv02 = (TextView) view.findViewById(R.id.push_items_list_tv_03);
			tv01.setText((CharSequence) map.get("name").toString());
			tv02.setText((CharSequence) map.get("je").toString());
			return view;
		}

	}

	class InitData_ implements Runnable
	{

		@Override
		public void run()
		{

			Looper.prepare();
			initData();
			Looper.loop();
		}

	}

	class RefreshData_ implements Runnable
	{

		@Override
		public void run()
		{

			Looper.prepare();
			refreshData();
			Looper.loop();
		}

	}

	class LoadMoreData_ implements Runnable
	{

		@Override
		public void run()
		{

			Looper.prepare();
			loadMoreData();
			Looper.loop();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{

	}

	@Override
	public void onRefresh()
	{
		new Thread(new RefreshData_()).start();

	}

	@Override
	public void onMore()
	{
		pageSize = pageSize + pageLoadMore;
		new Thread(new LoadMoreData_()).start();

	}

}
